<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>WeChat OAuth Test</title>
    <style>
        body { font-family: sans-serif; padding: 20px; }
        label { display: block; margin-bottom: 5px; }
        input[type="text"] { width: 250px; padding: 8px; margin-bottom: 15px; }
        button { padding: 10px 15px; cursor: pointer; }
        #message { margin-top: 15px; color: red; }
    </style>
</head>
<body>

    <h1>WeChat OAuth Login Test</h1>

    <div>
        <label for="inviteCode">Invite Code (Optional):</label>
        <input type="text" id="inviteCode" placeholder="Enter invite code if any">
    </div>

    <button id="loginButton">Login with WeChat</button>

    <div id="message"></div>

    <script>
        document.getElementById('loginButton').addEventListener('click', function() {
            const inviteCode = document.getElementById('inviteCode').value.trim();
            const messageDiv = document.getElementById('message');
            messageDiv.textContent = 'Fetching WeChat URL...';

            let apiUrl = 'https://api-mh.xianxia99.com/api/wechat/oauth_url';
            if (inviteCode) {
                apiUrl += '?invite_code=' + encodeURIComponent(inviteCode);
            }

            fetch(apiUrl)
                .then(response => {
                    if (!response.ok) {
                        throw new Error(`HTTP error! status: ${response.status}`);
                    }
                    return response.json();
                })
                .then(data => {
                    if (data.code === 200 && data.data && data.data.url) {
                        messageDiv.textContent = 'Redirecting to WeChat...';
                        window.location.href = data.data.url;
                    } else {
                        messageDiv.textContent = 'Error fetching URL: ' + (data.msg || 'Unknown error');
                        console.error('API Error:', data);
                    }
                })
                .catch(error => {
                    messageDiv.textContent = 'Failed to fetch WeChat URL: ' + error.message;
                    console.error('Fetch Error:', error);
                });
        });
    </script>

</body>
</html> 